﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using LocalDatabase.Model;
using LocalDatabase.ViewModel;
using System.Windows.Controls.Primitives;
using Microsoft.Phone.Tasks;

namespace FindMe.ViewModels
{
    public partial class FavoriteLocations : PhoneApplicationPage
    {
        public FavoriteLocations()
        {
            InitializeComponent();

            //// Set the page DataContext property to the ViewModel.
            this.DataContext = App._FavoriteLocationsViewModel;
        }


        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            // Save changes to the database.

            App._FavoriteLocationsViewModel.SaveChangesToDB();
        }


        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            var menuItem = (MenuItem)sender;
            var tag = menuItem.Tag.ToString();
        //    //MessageBox.Show(tag);

            if (tag == "delete")
            {
                MessageBox.Show(tag);
                //var button = sender as Button;

                if (menuItem != null)
                {
                    // Get a handle for the to-do item bound to the button.
                    //ToDoItem toDoForDelete = button.DataContext as ToDoItem;

                    FavoriteLocation Borrar = menuItem.DataContext as FavoriteLocation;

                    
                    App._FavoriteLocationsViewModel.DeleteLocation(Borrar);
                }

                // Put the focus back to the main page.
                this.Focus();
            }
            else
                MessageBox.Show(tag);
        }



        private void btnHome_Tap(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri(App.PathMain, UriKind.Relative));
        }

        private void btnInbox_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri(App.PathConversations, UriKind.Relative));
        }

        private void btnRequests_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri(App.PathNotifications, UriKind.Relative));
        }

      /*  private void Ok_Click(object sender, EventArgs e)
        {
            FavoriteLocation newLocation = new FavoriteLocation()
            {
                Name = "prueba de funcionalidad",
                Location = "0000000000000000"
            };

            // Add the item to the ViewModel.
            App._FavoriteLocationsViewModel.AddLocation(newLocation);
            //MessageBox.Show("MessageReceived!");
        }*/

        private void Cancel_Click(object sender, EventArgs e)
        {
            // trying to use this button to call the popup
            Popup locationName;
            locationName= new Popup();
            locationName.IsOpen = true;
            locationName.HorizontalOffset = 100;
            locationName.VerticalOffset = 25;
            locationName.Closed += (s1, e1) => { };
        }

        private void btnGoToFavLocation_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var button = (Button)sender;
                FavoriteLocation location = button.DataContext as FavoriteLocation;

                BingMapsDirectionsTask map = new BingMapsDirectionsTask()
                {
                    End = new LabeledMapLocation() { Label = location.Location }
                };
                map.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}